<template>
	<div class="diy_edit vip_level_edits" id="vip_level_edit">
	  <el-form
		:model="form"
		ref="ruleFormRef"
		:rules="rules"
		label-width="auto"
		class="edit_vip_level_form"
		size="large"
	  >
	    <el-row class="row_e">
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="等级名称" prop="name">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="折扣数" prop="discount">
				<el-input-number v-model="form.discount" :precision="2" :step="0.01" :min="0.01" :max="1"></el-input-number>
				</el-form-item>
			</el-col>
		</el-row>
		<div class="footer">
		  <el-button type="primary" @click="Onsubmit" size="large"
			>提交</el-button
		  >
		  <el-button @click="cancel()" size="large">取消</el-button>
		</div>
	  </el-form>
	</div>
  </template>
  
  <script setup>
  import { ElMessage } from "element-plus";
  import { ref, onMounted } from "vue";
  import { $post, $get } from "@/api/Api.js";
  import storageHelper from "@/libs/StorageHelper";
  import { useStore } from "vuex";
  import { useRouter } from "vue-router";
  import {
	$get_auth,
	func,
	paramsFun,
	$fullUrl,
	formatDate,
	$banseUrl,
	$check_action,
	$getUrlParams,
	filterSensitiveWords,
  } from "@/utils/utils.js";
  let router = useRouter();
  let store = useStore();
  let token = storageHelper.getToken() || null;
  let UserInfo = storageHelper.getUserInfo();
  let vip_level_id = $getUrlParams().vip_level_id;
  let url_add = ref("/api/vip_level/add");
  let url_set = ref("/api/vip_level/set");
  let url_get_obj = ref("/api/vip_level/get_obj");
  let title = ref("vip_level");
  let field = ref("vip_level_id");
  
  let form = ref({
	vip_level_id: 0,
    name: "",
	discount: 0.0
  });
  
  let ruleFormRef = ref(null);
  let rules = ref({
	name: [
	  {
		required: true,
		message: "等级名称不能为空",
		trigger: "blur",
	  },
	],
  });
  
  // 点击取消
  let cancel = () => {
	router.go(-1);
  };
  // 点击提交
  let Onsubmit = async () => {
	let valid = await ruleFormRef.value.validate();
	if (valid) {
	  if(vip_level_id){
		  // 编辑
		  delete form.value.update_time;
		  $post(url_set.value+"?vip_level_id=" + vip_level_id, form.value).then((res) => {
			  if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("/home/vip_level/table");
			  ElMessage.success("提交成功！");
			  } else if (res.error) {
			  ElMessage.error(res.error.message);
			  }
		  });
	   
	  }else{
		  // 新增
		  $post(url_add.value, form.value).then((res) => {
			  if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("/home/vip_level/table");
			  ElMessage.success("提交成功！");
			  } else if (res.error) {
			  ElMessage.error(res.error.message);
			  }
		  });
  
	  }
	} else {
	  ElMessage.error(msg);
	}
  };
  // 获取详情
  let get_vip_level_obj = async () => {
	var list = await $get("/api/vip_level/get_obj?vip_level_id=" + vip_level_id);
	form.value = list.result.obj;
  };
  onMounted(() => {
	if(vip_level_id){
	  get_vip_level_obj();
	}
  });
  </script>
  
  <style scoped lang="less">
  .diy_edit {
	width: 100%;
	.edit_vip_level_form {
	  max-width: 60%;
	  margin: 90px auto;
	  .fn {
		width: 100%;
	  }
	}
	img {
	  width: 100px;
	  height: 100px;
	}
	.edit_list_vip_level_sell {
	  width: 80%;
	  margin: 90px auto;
	}
	.footer {
	  text-align: center;
	}
  }
  </style>
  